package com.linfnn.demo.Controller;

import com.linfnn.demo.Model.Dietary;
import com.linfnn.demo.Service.impl.DietaryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
public class FunctionDietary {

    @Autowired
    private DietaryServiceImpl dietaryService;

    //  这是管理页面，同时可以刷新数据，进行操作后跳到这里会重新获取数据，从而刷新页面
    @RequestMapping(value = "/manage_dietaries")
    public ModelAndView showAll() {
        List<Dietary> dietaries = dietaryService.list();
        //        返回的页面
        ModelAndView mav = new ModelAndView("/managehtml/dietaryManage.html");
        //        页面和对应的数据结合
        mav.addObject("results", dietaries);
        return mav;
    }

    //
    @PostMapping("/search/Dietary")
    public ModelAndView search(@RequestParam("Code") String Code) {
        Dietary dietary = dietaryService.getDietaryByCode(Code);
        ModelAndView mav = new ModelAndView("/search/index4.html");
        mav.addObject("results", dietary);
        return mav;
    }

    //    注意这里返回应该更换为html，不能再去直接刷新获取整个数据库的数据， 并且这里的路由为searchadmin，前面有一个search，两者不一样
    @PostMapping("/searchadmin/Dietary")
    public ModelAndView searchadmin(@RequestParam("Code") String Code) {
        Dietary dietary = dietaryService.getDietaryByCode(Code);
        ModelAndView mav = new ModelAndView("/managehtml/dietaryManage.html");
        mav.addObject("results", dietary);
        return mav;
    }


    @RequestMapping("/delete/Dietary")
    public ModelAndView deleteDietary(@RequestParam("Code") String Code) {
        // Delete assessment logic here
        if (Code.isEmpty()){
            System.out.println("delete输入为空");
        }
        if(dietaryService.removeById(Code)){
            System.out.println("删除成功");
        };

        ModelAndView mav = new ModelAndView("redirect:/manage_dietaries");
        return mav;
    }


    //    这里点击更新后，会跳转到xxxUpdata.html文件里面去，然后再执行更新
    @RequestMapping("/updatehtml/Dietary")
    public ModelAndView updateDietaryhtml() {
        return new ModelAndView("redirect:/updatahtml/dietaryUpdata.html");
    }

    @RequestMapping("/update/Dietary")
    public ModelAndView updateDietary(@RequestParam("Code") String code,
                                            @RequestParam("FoodName") String foodName,
                                            @RequestParam("Type") String type,
                                            @RequestParam("EnergyPer100g") String energyPer100g,
                                            @RequestParam("ProteinContent") String proteinContent,
                                            @RequestParam("FatContent") String fatContent,
                                            @RequestParam("SuggestedGroup") String suggestedGroup,
                                            @RequestParam("Remarks") String remarks){
        // Update assessment logic here
//    这里逻辑比较复杂，为了方便，这里使用的是先删除对应数据，然后插入该数据的操作
        if(dietaryService.removeById(code)){
            System.out.println("更新——删除成功");
        };

        Dietary dietary = new Dietary();
        dietary.setCode(code);
        dietary.setFoodName(foodName);
        dietary.setType(type);
        dietary.setEnergyPer100g(energyPer100g);
        dietary.setProteinContent(proteinContent);
        dietary.setFatContent(fatContent);
        dietary.setSuggestedGroup(suggestedGroup);
        dietary.setRemarks(remarks);



        if(dietaryService.save(dietary)){
            System.out.println("更新——添加成功");
        };

        return new ModelAndView("redirect:/manage_dietaries");
    }



    @RequestMapping("/add/Dietary")
    public ModelAndView addDietary(@RequestParam("Code") String code,
                                      @RequestParam("FoodName") String foodName,
                                      @RequestParam("Type") String type,
                                      @RequestParam("EnergyPer100g") String energyPer100g,
                                      @RequestParam("ProteinContent") String proteinContent,
                                      @RequestParam("FatContent") String fatContent,
                                      @RequestParam("SuggestedGroup") String suggestedGroup,
                                      @RequestParam("Remarks") String remarks){
        // Add assessment logic here
        Dietary dietary = new Dietary();
        dietary.setCode(code);
        dietary.setFoodName(foodName);
        dietary.setType(type);
        dietary.setEnergyPer100g(energyPer100g);
        dietary.setProteinContent(proteinContent);
        dietary.setFatContent(fatContent);
        dietary.setSuggestedGroup(suggestedGroup);
        dietary.setRemarks(remarks);



        if(dietaryService.save(dietary)){
            System.out.println("添加成功");
        };
        return new ModelAndView("redirect:/manage_dietaries");
    }

}

